<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
			style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" label="ID" width="60">
			</el-table-column>

			<el-table-column prop="title" label="标题" width="180" sortable>
			</el-table-column>
			<el-table-column prop="value" label="值" width="180" sortable>
			</el-table-column>
			<el-table-column prop="sequence" label="序列" min-width="200" sortable>
			</el-table-column>
			<el-table-column prop="intro" label="简介" min-width="200" sortable>
			</el-table-column>
			<el-table-column prop="parent.title" label="所属数据" min-width="200" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
				:total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑界面
		:visible.sync="outerVisible" 新版本打开对话框
		v-model="dictionaryItemFormVisible" 老版本打开对话框
		-->
		<el-dialog title="新增或修改" :visible.sync="dictionaryItemFormVisible" :close-on-click-modal="false">
			<el-form :model="dictionaryItem" label-width="80px" :rules="dictionaryItemFormRules"
				ref="dictionaryItemForm">
				<el-form-item label="标题" prop="title">
					<el-input v-model="dictionaryItem.title" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="值" prop="value">
					<el-input v-model="dictionaryItem.value" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="序列" prop="sequence">
					<el-input v-model="dictionaryItem.sequence" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="简介" prop="intro">
					<el-input type="textarea" v-model="dictionaryItem.intro"></el-input>
				</el-form-item>
				<el-form-item label="所属数据" prop="parent">
					<!-- value-key="id" 必须写 -->
					<el-select v-model="dictionaryItem.parent" clearable value-key="id" placeholder="请选择">
						<!--下拉选项
						  :key="" 唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值 
						-->
						<el-option v-for="item in dictionaries" :key="item.id" :label="item.title" :value="item">
							<span style="float: left">{{ item.title }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.intro }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dictionaryItemFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="dictionaryItemLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
export default {
	data() {
		return {
			//分页相关
			query: {
				currentPage: 1,
				pageSize: 10,
				keyword: ''
			},
			pageInfo: {
				total: 0,
				rows: []
			},
			listLoading: false,
			sels: [],//列表选中列

			dictionaryItemFormVisible: false,//新增界面是否显示
			dictionaryItemLoading: false,
			dictionaryItemFormRules: {
				key: [
					{ required: true, message: '请输入键', trigger: 'blur' }
				],
				value: [
					{ required: true, message: '请输入值', trigger: 'blur' }
				]
			},
			//新增界面数据
			dictionaryItem: {
				id: null,
				title: '',
				value: '',
				sequence: '',
				intro: '',
				parent: {}
			},
			// 选取数据明细所属的数据
			dictionaries: []
		}
	},
	methods: {
		search() {
			//重置当前页为第一页
			this.query.currentPage = 1;
			this.getDictionaryItems();
		},
		handleCurrentChange(val) {
			this.query.currentPage = val;
			this.getDictionaryItems();
		},
		//获取用户列表
		getDictionaryItems() {
			this.listLoading = true; //发起请求之前显示加载条
			//发起请求获取数据
			this.$http.post("/dictionaryItem", this.query)
				.then(result => {
					result = result.data;
					if (result.success) {
						this.pageInfo = result.data;
					} else {
						this.$message({
							message: '获取分页数据失败！' + result.message,
							type: 'error'
						});
					}
					this.listLoading = false;
				})
				.catch(result => {
					this.$message({
						message: '系统错误！',
						type: 'error'
					});
				})
		},
		//删除
		handleDel: function (index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				//NProgress.start();
				let para = row.id;
				this.$http.delete("/dictionaryItem/" + para).then((res) => {
					this.listLoading = false;
					//NProgress.done();
					this.$message({
						message: '删除成功',
						type: 'success'
					});
					this.getDictionaryItems();
				});
			}).catch(() => {

			});
		},
		//显示编辑界面
		handleEdit: function (index, row) {
			this.dictionaryItemFormVisible = true;
			this.getDictionaries();
			this.dictionaryItem = Object.assign({}, row); //回显
		},
		//显示新增界面
		handleAdd: function () {
			this.dictionaryItemFormVisible = true;
			this.dictionaryItem = {
				id: null,
				title: '',
				value: '',
				sequence: '',
				intro: '',
				parent: {}
			}
			this.getDictionaries();
		},

		getDictionaries() {
			this.$http.get("/dictionary")
				.then(result => {
					result = result.data;
					if (result.success) {
						this.dictionaries = result.data;
					}
				})
				.catch(result => {
					this.$message({
						message: '获取失败,' + result.message,
						type: 'error'
					});
				})
		},
		//编辑
		save: function () {
			this.$refs.dictionaryItemForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.dictionaryItemLoading = true;
						let para = Object.assign({}, this.dictionaryItem);
						this.$http.put("/dictionaryItem", para).then((res) => {
							res = res.data;

							if (res.success) {
								this.$message({
									message: '保存成功',
									type: 'success'
								});

								this.dictionaryItemFormVisible = false; //关闭对话框

								this.$refs['dictionaryItemForm'].resetFields(); //重置表单
								this.getDictionaryItems(); //重新加载数据
							} else {
								this.$message({
									message: '保存失败,' + res.message,
									type: 'error'
								});
							}
							this.dictionaryItemLoading = false; //关闭加载条
						});
					});
				}
			});
		},

		selsChange: function (sels) {
			this.sels = sels;
		},
		//批量删除
		batchRemove: function () {
			let ids = this.sels.map(item => item.id);
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				this.$http.patch("/dictionaryItem", ids).then((res) => {
					this.listLoading = false;
					this.$message({
						message: '删除成功',
						type: 'success'
					});
					this.getDictionaryItems();
				});
			}).catch(() => {

			});
		}
	},
	mounted() {
		this.getDictionaryItems();
	}
}

</script>

<style scoped>
</style>